//
//  StockMonitorListController.m
//  Stock
//
//  Created by blacksun on 11-7-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StockMonitorListController.h"


@implementation StockMonitorListController
@synthesize stockMonitorService;
@synthesize title3Label;
@synthesize title4Label;

-(void)queryStocks{
    NSString *uri = [NSString stringWithFormat:@"dig/fund?id=%@&start=%d",key,currentPage*20];
    [self.quotationCenterService queryQuotationStocksWithUri:uri];
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        
        UIColor *clearColor = [UIColor clearColor];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(4.0, 10.0, 70.0, 20.0)];
        nameLabel.backgroundColor = clearColor;
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.tag = kStockNameTag;
        UILabel *todayPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(70.0, 10.0, 70.0, 20.0)];
        todayPriceLabel.backgroundColor = clearColor;
        todayPriceLabel.tag = kStockTodayPriceTag;
        todayPriceLabel.textAlignment = UITextAlignmentRight;
        
        UILabel *changeRateLabel = [[UILabel alloc] initWithFrame:CGRectMake(142.0, 10.0, 80.0, 20.0)];
        changeRateLabel.backgroundColor = clearColor;
        changeRateLabel.tag = kStockChangeRateTag;
        changeRateLabel.textAlignment = UITextAlignmentRight;
        
        UILabel *fNetLabel = [[UILabel alloc] initWithFrame:CGRectMake(230.0, 10.0, 85.0, 20.0)];
        fNetLabel.backgroundColor = clearColor;
        fNetLabel.tag = kStockChangePriceTag;
        fNetLabel.textAlignment = UITextAlignmentRight;
        
        
        [cell.contentView addSubview:nameLabel];
        [cell.contentView addSubview:todayPriceLabel];
        [cell.contentView addSubview:changeRateLabel];
        [cell.contentView addSubview:fNetLabel];
        
        [nameLabel release];
        [todayPriceLabel release];
        [changeRateLabel release];
        [fNetLabel release];
        
    }
    NSInteger index = [indexPath row];
    NSString *imageName = @"cellbg2.png";
    if (index%2==1) {
        imageName = @"cellbg1.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    QuotationStock *currentStock = [quotationStocks objectAtIndex:index];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:kStockNameTag];
    UILabel *todyLabel = (UILabel *)[cell viewWithTag:kStockTodayPriceTag];
    UILabel *fNetLabel = (UILabel *)[cell viewWithTag:kStockChangePriceTag];
    UILabel *rateLabel = (UILabel *)[cell viewWithTag:kStockChangeRateTag];
    
    NSString *text3 = [currentStock.changedRate retain];
    if ([key hasSuffix:@"3"]||[key hasSuffix:@"10"]||[key hasSuffix:@"60"]) {
        currentStock.changedRate = currentStock.currentRate;//text3 = currentStock.currentRate;//title3Label.text = [NSString stringWithFormat:@"3日%@",label3String];
    }
    NSString *text4 = currentStock.netamount;
    if ([key isEqualToString:@"FI"]) {
    }else if([key isEqualToString:@"FO"]){
    }else if([key isEqualToString:@"FFI"]){
    }else if([key isEqualToString:@"FFO"]){
    }else if([key isEqualToString:@"FCI"]){
        text3 = [currentStock.days stringValue];
    }else if([key isEqualToString:@"FCO"]){
        text3 = [currentStock.days stringValue];
    }else if([key isEqualToString:@"FIR"]){
        text4 = currentStock.ratioamount;
    }else if([key isEqualToString:@"FOR"]){
        text4 = currentStock.ratioamount;
    }else if([key isEqualToString:@"FFIR"]){
        text4 = currentStock.ratioamount;
    }else if([key isEqualToString:@"FFOR"]){
        text4 = currentStock.ratioamount;
    }else if([key isEqualToString:@"FCIR"]){
        text3 = [currentStock.days stringValue];
        text4 = currentStock.ratioamount;
    }else if([key isEqualToString:@"FCOR"]){
        text3 = [currentStock.days stringValue];
        text4 = currentStock.ratioamount;
    }
    
    todyLabel.textColor = [UIColor whiteColor];
    rateLabel.textColor = [text3 hasPrefix:@"-"]?[UIColor greenColor]:[UIColor redColor];
    fNetLabel.textColor = [text4 hasPrefix:@"-"]?[UIColor greenColor]:[UIColor redColor];
    
    nameLabel.text = currentStock.name;
    if (currentStock.closingPrice && currentStock.closingPrice != 0) {
        todyLabel.text = [currentStock.closingPrice stringValue];
        rateLabel.text = text3; 
        fNetLabel.text = text4;
    }else{
        todyLabel.text = @"--";
        rateLabel.text = @"--";  
        fNetLabel.text = @"--";
    }
    [text3 release];
    return cell;
}

-(void)viewDidLoad{
    
    self.stockMonitorService = [[[StockMonitorService alloc] initWithDelegate:self] autorelease];
    [super viewDidLoad];
    NSString *label3String = @"涨幅";
    if ([key hasSuffix:@"3"]) {
        title3Label.text = [NSString stringWithFormat:@"3日%@",label3String];
    }
    if ([key hasSuffix:@"10"]) {
        title3Label.text = [NSString stringWithFormat:@"10日%@",label3String];
    }
    if ([key hasSuffix:@"60"]) {
        title3Label.text = [NSString stringWithFormat:@"60日%@",label3String];
    }
    
    if ([key hasPrefix:@"FI"]) {
        title4Label.text = @"资金净流入";
    }else if([key hasPrefix:@"FO"]){
        title4Label.text = @"资金净流出";
    }else if([key hasPrefix:@"FFI"]){
        title4Label.text = @"主力净流入";
    }else if([key hasPrefix:@"FFO"]){
        title4Label.text = @"主力净流出";
    }/*else if([key isEqualToString:@"FCI"]){
        title3Label.text = @"连续天数";
        title4Label.text = @"净流入";
    }else if([key isEqualToString:@"FCO"]){
        title3Label.text = @"连续天数";
        title4Label.text = @"净流出";
    }else if([key isEqualToString:@"FIR"]){
        title4Label.text = @"净流入率";
    }else if([key isEqualToString:@"FOR"]){
        title4Label.text = @"净流出率";
    }else if([key isEqualToString:@"FFIR"]){
        title4Label.text = @"净流入率";
    }else if([key isEqualToString:@"FFOR"]){
        title4Label.text = @"净流出率";
    }else if([key isEqualToString:@"FCIR"]){
        title3Label.text = @"连续天数";
        title4Label.text = @"净流入率";
    }else if([key isEqualToString:@"FCOR"]){
        title3Label.text = @"连续天数";
        title4Label.text = @"净流出率";
    }*/
}
-(void)viewDidUnload{
    [super viewDidUnload];
    [stockMonitorService cancleAllRequest];
    self.stockMonitorService = nil;
    self.title3Label = nil;
    self.title4Label = nil;
}
-(void)dealloc{
    [stockMonitorService cancleAllRequest];
    self.stockMonitorService = nil;
    self.title3Label = nil;
    self.title4Label = nil;
    [super dealloc];
}


@end
